package com.jwgf.smarowindingmachine_unity.ui;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.jwgf.adapter.MultiItemTypeAdapter;
import com.jwgf.common.AppContext;
import com.jwgf.common.ViewCache;
import com.jwgf.faultnavigation.FaultNavigationButtonDelegate;
import com.jwgf.faultnavigation.FaultNavigationMap;
import com.jwgf.faultnavigation.FaultNavigationTitle;
import com.jwgf.faultnavigation.FaultNavigationTitleDelegate;
import com.jwgf.smarowindingmachine_unity.R;

import java.util.ArrayList;
import java.util.List;

public class MachineStateFaultNavigationFragment extends BaseFragment {
    private View v;
    private RecyclerView recyclerView;
    private MultiItemTypeAdapter<Object> adapter;

    private List<Object> list = new ArrayList<>();

    @Nullable
    @Override
    public View onCreateView(final LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        v = ViewCache.getInstance().getView(R.layout.recycler_view);
        recyclerView = (RecyclerView) v.findViewById(R.id.recycler);
        // 单锭报警标题
        list.add(new FaultNavigationTitle(R.string.spindle_fault, R.string.spindle_fault_attention));
        // 单锭报警
        list.add(FaultNavigationMap.getInstance().get(0x630102));
        list.add(FaultNavigationMap.getInstance().get(0x630103));
        list.add(FaultNavigationMap.getInstance().get(0x630104));
        list.add(FaultNavigationMap.getInstance().get(0x630106));
        list.add(FaultNavigationMap.getInstance().get(0x630107));
        list.add(FaultNavigationMap.getInstance().get(0x630108));
        list.add(FaultNavigationMap.getInstance().get(0x630109));
        list.add(FaultNavigationMap.getInstance().get(0x63010A));
        list.add(FaultNavigationMap.getInstance().get(0x63010B));
        list.add(FaultNavigationMap.getInstance().get(0x63010E));
        list.add(FaultNavigationMap.getInstance().get(0x630201));
        list.add(FaultNavigationMap.getInstance().get(0x670200));

        // 主机报警标题
        list.add(new FaultNavigationTitle(R.string.machine_fault, R.string.none));
        // 主机报警
        list.add(FaultNavigationMap.getInstance().get(0x0200FC02));
        list.add(FaultNavigationMap.getInstance().get(0x0200FC09));
        list.add(FaultNavigationMap.getInstance().get(0x0200FC0B));
        list.add(FaultNavigationMap.getInstance().get(0x0200FC0C));
        list.add(FaultNavigationMap.getInstance().get(0x0200FC0D));

        // 管纱处理标题
        list.add(new FaultNavigationTitle(R.string.cbf_fault, R.string.cbf_fault_attention));
        // 管纱处理报警
        list.add(FaultNavigationMap.getInstance().get(0x04013308));
        list.add(FaultNavigationMap.getInstance().get(0x0401330A));
        list.add(FaultNavigationMap.getInstance().get(0x0401330B));
        list.add(FaultNavigationMap.getInstance().get(0x0401330C));
        list.add(FaultNavigationMap.getInstance().get(0x0401330D));
        list.add(FaultNavigationMap.getInstance().get(0x0401330E));
        list.add(FaultNavigationMap.getInstance().get(0x0401330F));
        list.add(FaultNavigationMap.getInstance().get(0x04014C03));

        // 小车报警标题
        list.add(new FaultNavigationTitle(R.string.doffer_fault, R.string.doffer_fault_attention));
        // 小车报警
        list.add(FaultNavigationMap.getInstance().get(0x760000));
        list.add(FaultNavigationMap.getInstance().get(0x760001));
        list.add(FaultNavigationMap.getInstance().get(0x760003));
        list.add(FaultNavigationMap.getInstance().get(0x760004));
        list.add(FaultNavigationMap.getInstance().get(0x760005));
        list.add(FaultNavigationMap.getInstance().get(0x760006));
        list.add(FaultNavigationMap.getInstance().get(0x760008));
        list.add(FaultNavigationMap.getInstance().get(0x76000B));
        list.add(FaultNavigationMap.getInstance().get(0x76000C));
        list.add(FaultNavigationMap.getInstance().get(0x76000D));
        list.add(FaultNavigationMap.getInstance().get(0x76000E));
        list.add(FaultNavigationMap.getInstance().get(0x76000F));
        list.add(FaultNavigationMap.getInstance().get(0x760100));
        list.add(FaultNavigationMap.getInstance().get(0x760101));
        list.add(FaultNavigationMap.getInstance().get(0x760102));
        list.add(FaultNavigationMap.getInstance().get(0x760103));
        list.add(FaultNavigationMap.getInstance().get(0x760104));
        list.add(FaultNavigationMap.getInstance().get(0x760105));
        list.add(FaultNavigationMap.getInstance().get(0x760106));
        list.add(FaultNavigationMap.getInstance().get(0x760109));

        adapter = new MultiItemTypeAdapter<>(AppContext.getInstance(), list);
        adapter.addItemViewDelegate(new FaultNavigationButtonDelegate());
        adapter.addItemViewDelegate(new FaultNavigationTitleDelegate());

        GridLayoutManager layoutManager = new GridLayoutManager(getActivity(), 6);

        layoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {

            @Override
            public int getSpanSize(int position) {
                if (position == 0 || position == 13 || position == 19 || position == 28) {
                    return 6;
                } else {
                    return 1;
                }
            }

        });

        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(adapter);

        return v;
    }
}
